package com.niiwoo.civet.trade.service.local.plan;

import com.niiwoo.civet.base.enums.FeeTypeEnum;
import com.niiwoo.civet.trade.constant.AmountHandleConstant;
import com.niiwoo.civet.trade.constant.DatePatternConstant;
import com.niiwoo.civet.trade.dao.entity.*;
import com.niiwoo.civet.trade.dao.mapper.*;
import com.niiwoo.civet.trade.dto.BorrowerRepaymentDTO;
import com.niiwoo.civet.trade.dto.RateConfigDTO;
import com.niiwoo.civet.trade.dto.common.BuildRepaymentDTO;
import com.niiwoo.civet.trade.dto.request.loan.RepaymentPlanDTO;
import com.niiwoo.civet.trade.enums.*;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.ProjectPackageLocalService;
import com.niiwoo.tripod.consumer.helper.Exceptions;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.*;

/**
 * 按月付息,到期还本
 *
 * @author liaojunjun
 *
 */
@Service
public class MonthlyInterestPaymentDue extends AbstractBorrowerInvestorRepaymentManager {

	@Autowired
	private SnowflakeIdWorker snowflakeIdWorker;

	@Autowired
	private ProjectBorrowerSummaryMapper projectBorrowerSummaryMapper;

	@Autowired
	private ProjectBorrowerPlanNewMapper projectBorrowerPlanNewMapper;

	@Autowired
	private ProjectBorrowerPlanDetailMapperExt projectBorrowerPlanDetailMapperExt;

	@Autowired
	private ProjectInvestorPlanMapper projectInvestorPlanMapper;

	@Autowired
	private InvestorCreditorMapperExt investorCreditorMapperExt;

	@Autowired
	private ProjectPackageLocalService projectPackageLocalService;

	@Override
	public boolean supports(InterestAccrualEnum interestAccrualEnum) {
		return InterestAccrualEnum.MONTHLY_INTEREST_PAYMENT_DUE.equals(interestAccrualEnum);
	}

	@Override
	public List<RepaymentPlanDTO> getBorrowerRepaymentPlan(BigDecimal amount, BigDecimal yearRate, Integer deadline,
			PeriodUnitEnum periodUnit, Date startDate, Integer leastCalculateInterestDays) {
		List<RepaymentPlanDTO> result = new ArrayList<RepaymentPlanDTO>();
		if (PeriodUnitEnum.MONTH != periodUnit) {
			Exceptions.throwBizException("TRD_700003");// 等额本息还款方式的期限单位不是月
		}
		// 月利率： 年化利率/12
		BigDecimal monthRate = yearRate.divide(BigDecimal.valueOf(AmountHandleConstant.YEAR_OF_MONTH),
				MathContext.DECIMAL128);

		for (int i = 1; i < deadline + 1; i++) {
			RepaymentPlanDTO repaymentPlanDTO = new RepaymentPlanDTO();
			repaymentPlanDTO.setPlanType(RepaymentTypeEnum.MONTHLY_INTEREST_PAYMENT_DUE.getValue());
			repaymentPlanDTO.setCurrentPeriod(i);
			repaymentPlanDTO.setTotalPeriod(deadline);
			repaymentPlanDTO.setInterest(monthRate.multiply(amount).setScale(
					AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN));

			repaymentPlanDTO.setRefundDate(DateUtils.addDays(DateUtils.addMonths(startDate, i), -1));
			if (i == deadline) {
				repaymentPlanDTO.setPrincipal(amount);
			} else {
				repaymentPlanDTO.setPrincipal(BigDecimal.ZERO);
			}
			repaymentPlanDTO.setPrincipalInterest(repaymentPlanDTO.getPrincipal().add(repaymentPlanDTO.getInterest()));

			result.add(repaymentPlanDTO);
		}

		return result;
	}

	@Override
	public List<RepaymentPlanDTO> getBorrowerRepaymentPlan(BuildRepaymentDTO project, List<RateConfigDTO> rateConfigDTOList) {
		List<RepaymentPlanDTO> result = new ArrayList<RepaymentPlanDTO>();
		PeriodUnitEnum periodUnit = project.getPeriodUnit();
		if (PeriodUnitEnum.MONTH != periodUnit) {
			Exceptions.throwBizException("TRD_700003");// 等额本息还款方式的期限单位不是月
		}
		// 月利率： 年化利率/12
		BigDecimal monthRate = project.getYearRate().divide(BigDecimal.valueOf(AmountHandleConstant.YEAR_OF_MONTH),
				MathContext.DECIMAL128);

		// 担保服务费
		BigDecimal guaranteeServiceFeeTotal = project.getAmount().multiply(project.getGuaranteeRate())
				.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);
		BigDecimal guaranteeServiceFee = BigDecimal.ZERO;

		// 平台管理费
		BigDecimal platformManageFeeTotal = project.getAmount().multiply(project.getManageRate())
				.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);
		BigDecimal platformManageFee = BigDecimal.ZERO;

		// lastAvailableGuaranteeServiceFee 最后一期有效担保服务费
		Integer lastAvailableGuaranteeServiceFee = rateConfigDTOList.size();
		// lastAvailablePlatformManageFee 最后一期有效平台管理费
		Integer lastAvailablePlatformManageFee = rateConfigDTOList.size();
		for(int i = rateConfigDTOList.size() - 1 ; i >= 0; i--){
			if(rateConfigDTOList.get(i).getGuaranteeServiceFeePercent().compareTo(BigDecimal.ZERO) > 0){
				lastAvailableGuaranteeServiceFee = rateConfigDTOList.get(i).getCurrentPeriod();
				break;
			}
		}
		for(int i = rateConfigDTOList.size() - 1 ; i >= 0; i--){
			if(rateConfigDTOList.get(i).getPlatformManageFeePercent().compareTo(BigDecimal.ZERO) > 0){
				lastAvailablePlatformManageFee = rateConfigDTOList.get(i).getCurrentPeriod();
				break;
			}
		}

		for (int i = 1; i < project.getDeadline() + 1; i++) {
			RepaymentPlanDTO repaymentPlanDTO = new RepaymentPlanDTO();
			repaymentPlanDTO.setPlanType(RepaymentTypeEnum.MONTHLY_INTEREST_PAYMENT_DUE.getValue());
			repaymentPlanDTO.setCurrentPeriod(i);
			repaymentPlanDTO.setTotalPeriod(project.getDeadline());
			repaymentPlanDTO.setInterest(monthRate.multiply(project.getAmount()).setScale(
					AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN));

			repaymentPlanDTO.setRefundDate(DateUtils.addDays(
					DateUtils.addMonths(
							project.getStartDate() == null ? new Date() : project.getStartDate(), i), -1));
			if (i == project.getDeadline()) {
				repaymentPlanDTO.setPrincipal(project.getAmount());
			} else {
				repaymentPlanDTO.setPrincipal(BigDecimal.ZERO);
			}
			repaymentPlanDTO.setPrincipalInterest(repaymentPlanDTO.getPrincipal().add(repaymentPlanDTO.getInterest()));

			repaymentPlanDTO.setGuaranteeServiceFee(BigDecimal.ZERO);
			repaymentPlanDTO.setPlatformManageFee(BigDecimal.ZERO);

			for (RateConfigDTO rateConfigDTO : rateConfigDTOList) {
				if (rateConfigDTO != null && rateConfigDTO.getCurrentPeriod() == i) {
					// 抹平管理费
					// lastAvailablePlatformManageFee 为最后一期有效平台管理费 最后一期有效平台管理费=收费用总收费减去前N期收费之和
					if (rateConfigDTO.getCurrentPeriod().equals(lastAvailablePlatformManageFee)) {
						repaymentPlanDTO.setPlatformManageFee(platformManageFeeTotal.subtract(platformManageFee));
					} else {
						repaymentPlanDTO.setPlatformManageFee(platformManageFeeTotal.multiply(
								rateConfigDTO.getPlatformManageFeePercent()).setScale(
								AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN));

						platformManageFee = platformManageFee.add(repaymentPlanDTO.getPlatformManageFee());
					}

					// 抹平担保服务费
					// lastAvailableGuaranteeServiceFee 为最后一期有效担保服务费 最后一次有效担保服务费=收费用总收费减去前N期收费之和
					if (rateConfigDTO.getCurrentPeriod().equals(lastAvailableGuaranteeServiceFee)) {
						repaymentPlanDTO.setGuaranteeServiceFee(guaranteeServiceFeeTotal.subtract(guaranteeServiceFee));
					} else {
						repaymentPlanDTO.setGuaranteeServiceFee(guaranteeServiceFeeTotal.multiply(
								rateConfigDTO.getGuaranteeServiceFeePercent()).setScale(
								AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN));

						guaranteeServiceFee = guaranteeServiceFee.add(repaymentPlanDTO.getGuaranteeServiceFee());
					}
					break;
				}
			}

			repaymentPlanDTO.setRepaymentTotalAmount(repaymentPlanDTO.getPrincipalInterest()
					.add(repaymentPlanDTO.getGuaranteeServiceFee()).add(repaymentPlanDTO.getPlatformManageFee()));

			result.add(repaymentPlanDTO);
		}

		return result;
	}

	@Override
	public BorrowerRepaymentDTO buildBorrowerRepaymentPlan(Project project,
			ProjectBorrowerSummary projectBorrowerSummary, List<ProjectRate> projectRateList) {
		BorrowerRepaymentDTO result = new BorrowerRepaymentDTO();

		PeriodUnitEnum periodUnit = PeriodUnitEnum.enumOf(project.getDeadlineUnit());
		if (PeriodUnitEnum.MONTH != periodUnit) {
			Exceptions.throwBizException("TRD_700003");// 按月付息,到期还本还款方式的期限单位不是月
		}

		projectBorrowerSummary.setTotalPeriod(project.getDeadline());
		projectBorrowerSummary
				.setRefundDate(DateUtils.addDays(DateUtils.addMonths(project.getFullSuccessTime(), 1), -1));
		projectBorrowerSummary.setAutoRefundDate(projectBorrowerSummary.getRefundDate());

		projectBorrowerSummaryMapper.insertSelective(projectBorrowerSummary);

		List<ProjectBorrowerPlanNew> projectBorrowerPlanNewList = new ArrayList<>();

		// 月利率： 年化利率/12
		BigDecimal monthRate = project.getBorrowRate().divide(BigDecimal.valueOf(AmountHandleConstant.YEAR_OF_MONTH),
				MathContext.DECIMAL128);

		// 担保服务费
		BigDecimal guaranteeServiceFeeTotal = !ProjectFeeChargeNodeEnum.GuaranteeFeeChargeNode.COLLECTION_OF_MONEY.getValue().
				equals(project.getGuaranteeFeeChargeNode()) ? project.getContractAmount().multiply(project.getGuaranteeRate())
				.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN) : BigDecimal.ZERO;
		BigDecimal guaranteeServiceFee = BigDecimal.ZERO;

		// 平台管理费
		BigDecimal platformManageFeeTotal = !ProjectFeeChargeNodeEnum.ManageFeeChargeNode.COLLECTION_OF_MONEY.getValue().
				equals(project.getManageFeeChargeNode()) ? project.getContractAmount().multiply(project.getManageRate())
				.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN) : BigDecimal.ZERO;
		BigDecimal platformManageFee = BigDecimal.ZERO;

		// 咨询服务费(按照合同金额*费率)
		BigDecimal consultingFeeTotal = !ProjectFeeChargeNodeEnum.AfterSeekAdviceServiceFeeChargeNode.COLLECTION_OF_MONEY.getValue().
				equals(project.getAfterSeekAdviceServiceFeeChargeNode()) ? project.getContractAmount() : BigDecimal.ZERO;


		// lastAvailableGuaranteeServiceFee 最后一期有效担保服务费
		Integer lastAvailableGuaranteeServiceFee = projectRateList.size();
		// lastAvailablePlatformManageFee 最后一期有效平台管理费
		Integer lastAvailablePlatformManageFee = projectRateList.size();

		for(int i = projectRateList.size() - 1 ; i >= 0; i--){
			if(projectRateList.get(i).getGuaranteeServiceFeePercent().compareTo(BigDecimal.ZERO) > 0){
				lastAvailableGuaranteeServiceFee = projectRateList.get(i).getCurrentPeriod();
				break;
			}
		}
		for(int i = projectRateList.size() - 1 ; i >= 0; i--){
			if(projectRateList.get(i).getPlatformManageFeePercent().compareTo(BigDecimal.ZERO) > 0){
				lastAvailablePlatformManageFee = projectRateList.get(i).getCurrentPeriod();
				break;
			}
		}

		BigDecimal currentGuaranteeServiceFee;
		BigDecimal currentPlatformManageFee;
		BigDecimal currentConsultingFee;
		for (int i = 1; i < project.getDeadline() + 1; i++) {
			Date refundDate = DateUtils.addDays(DateUtils.addMonths(project.getFullSuccessTime(), i), -1);

			ProjectBorrowerPlanNew projectBorrowerPlanNew = buildProjectBorrowerPlanNew(snowflakeIdWorker.nextId(),
					project.getProjectId(), project.getBorrowUserId(), project.getBorrowAccountId(), refundDate, i, periodUnit.getValue(), project.getDeadline());
			projectBorrowerPlanNewMapper.insertSelective(projectBorrowerPlanNew);


			for (ProjectRate projectRate : projectRateList) {
				if (projectRate != null && projectRate.getCurrentPeriod() == i) {
					// 抹平管理费
					// lastAvailablePlatformManageFee 为最后一期有效平台管理费 最后一期有效平台管理费=收费用总收费减去前N期收费之和
					if(ProjectFeeChargeNodeEnum.ManageFeeChargeNode.REPAYMENT_FIRST.getValue().
							equals(project.getManageFeeChargeNode())){
						currentPlatformManageFee = i == 1 ? platformManageFeeTotal : BigDecimal.ZERO;
					}else{
						if (projectRate.getCurrentPeriod().equals(lastAvailablePlatformManageFee)) {
							currentPlatformManageFee = platformManageFeeTotal.subtract(platformManageFee);
						} else {
							currentPlatformManageFee = platformManageFeeTotal.multiply(
									projectRate.getPlatformManageFeePercent()).setScale(
									AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);

							platformManageFee = platformManageFee.add(currentPlatformManageFee);
						}
					}


					// 抹平担保服务费getGuaranteeFeeChargeNode
					// lastAvailableGuaranteeServiceFee 为最后一期有效担保服务费 最后一次有效担保服务费=收费用总收费减去前N期收费之和
					if(ProjectFeeChargeNodeEnum.GuaranteeFeeChargeNode.REPAYMENT_FIRST.getValue().
							equals(project.getGuaranteeFeeChargeNode())){
						currentGuaranteeServiceFee = i == 1 ? guaranteeServiceFeeTotal : BigDecimal.ZERO;
					}else{
						if (projectRate.getCurrentPeriod().equals(lastAvailableGuaranteeServiceFee)) {
							currentGuaranteeServiceFee = guaranteeServiceFeeTotal.subtract(guaranteeServiceFee);
						} else {
							currentGuaranteeServiceFee = guaranteeServiceFeeTotal.multiply(
									projectRate.getGuaranteeServiceFeePercent()).setScale(
									AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);

							guaranteeServiceFee = guaranteeServiceFee.add(currentGuaranteeServiceFee);
						}
					}

					if(ProjectFeeChargeNodeEnum.AfterSeekAdviceServiceFeeChargeNode.REPAYMENT_FIRST.getValue().
							equals(project.getAfterSeekAdviceServiceFeeChargeNode())){
						currentConsultingFee = i == 1 ? project.getConsultingFee() : BigDecimal.ZERO;
					}else{
						currentConsultingFee = consultingFeeTotal.multiply(
								projectRate.getAfterSeekAdviceServiceFeeRate()).setScale(
								AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);
					}

					if(currentGuaranteeServiceFee.compareTo(BigDecimal.ZERO) != 0) {
						ProjectBorrowerPlanDetail guaranteeServiceFeeDetail = buildProjectBorrowerPlanDetail(snowflakeIdWorker.nextId(),
								project.getProjectId(), project.getBorrowUserId(), projectBorrowerPlanNew.getId(),
								FeeTypeEnum.GUARANTEE_FEE.getValue().intValue(), currentGuaranteeServiceFee);
						projectBorrowerPlanDetailMapperExt.insertSelective(guaranteeServiceFeeDetail);
					}

					if(currentPlatformManageFee.compareTo(BigDecimal.ZERO) != 0) {
						ProjectBorrowerPlanDetail platformManageFeeDetail = buildProjectBorrowerPlanDetail(snowflakeIdWorker.nextId(),
								project.getProjectId(), project.getBorrowUserId(), projectBorrowerPlanNew.getId(),
								FeeTypeEnum.MANAGEMENT_FEE.getValue().intValue(), currentPlatformManageFee);
						projectBorrowerPlanDetailMapperExt.insertSelective(platformManageFeeDetail);
					}

					if(currentConsultingFee.compareTo(BigDecimal.ZERO) != 0){
						ProjectBorrowerPlanDetail consultingFeeDetail = buildProjectBorrowerPlanDetail(snowflakeIdWorker.nextId(),
								project.getProjectId(), project.getBorrowUserId(), projectBorrowerPlanNew.getId(),
								FeeTypeEnum.CONSULTING_FEE.getValue().intValue(), currentConsultingFee);
						projectBorrowerPlanDetailMapperExt.insertSelective(consultingFeeDetail);
					}
					break;
				}
			}


			// 按月付息到期还本只有最后一次才还本金
			if (i == (project.getDeadline())) {
				ProjectBorrowerPlanDetail capitalDetail = buildProjectBorrowerPlanDetail(snowflakeIdWorker.nextId(), project.getProjectId(),
						project.getBorrowUserId(), projectBorrowerPlanNew.getId(), FeeTypeEnum.CAPITAL.getValue().intValue(), project.getContractAmount());
				projectBorrowerPlanDetailMapperExt.insertSelective(capitalDetail);
			}

			// 利息
			BigDecimal currentInterest = project.getContractAmount().multiply(monthRate)
					.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN);
			ProjectBorrowerPlanDetail interestDetail = buildProjectBorrowerPlanDetail(snowflakeIdWorker.nextId(),
					project.getProjectId(), project.getBorrowUserId(), projectBorrowerPlanNew.getId(), FeeTypeEnum.INTEREST.getValue().intValue(), currentInterest);
			projectBorrowerPlanDetailMapperExt.insertSelective(interestDetail);

			projectBorrowerPlanNewList.add(projectBorrowerPlanNew);
		}

		result.setProjectBorrowerSummary(projectBorrowerSummary);
		result.setProjectBorrowerPlanNewList(projectBorrowerPlanNewList);
		return result;

	}

	@Override
	public List<ProjectInvestorPlan> buildInvestorRepaymentPlan(Project project,
			List<ProjectInvestOrder> projectInvestOrderList, BorrowerRepaymentDTO borrowerRepaymentDTO) {
		List<ProjectInvestorPlan> result = new ArrayList<ProjectInvestorPlan>();

		PeriodUnitEnum periodUnit = PeriodUnitEnum.enumOf(project.getDeadlineUnit());
		if (PeriodUnitEnum.MONTH != periodUnit) {
			Exceptions.throwBizException("TRD_700003");// 按月付息,到期还本还款方式的期限单位不是月
		}

		// 月利率： 年化利率/12
		BigDecimal monthRate = project.getBorrowRate().divide(BigDecimal.valueOf(AmountHandleConstant.YEAR_OF_MONTH),
				MathContext.DECIMAL128);

		Map<Integer, BigDecimal> investCapitalTotal = new HashMap<>();
		Map<Integer, BigDecimal> investInterestTotal = new HashMap<>();

		boolean isPackagedProject = projectPackageLocalService.isPackagedProject(project.getProjectId());

		List<ProjectBorrowerPlanNew> projectBorrowerPlanNewList = borrowerRepaymentDTO.getProjectBorrowerPlanNewList();
		for (ProjectInvestOrder projectInvestOrder : projectInvestOrderList) {
			if (projectInvestOrder != null
					&& ProjectInvestOrderStatusEnum.SUCCESS.getValue().equals(projectInvestOrder.getStatus())) {

				Date firstReceivedDate = null;
				BigDecimal firstInterest = null;
				Date expectOverDate = null;
				BigDecimal investorInterest = BigDecimal.ZERO;

				for (ProjectBorrowerPlanNew projectBorrowerPlanNew : projectBorrowerPlanNewList) {
					ProjectInvestorPlan projectInvestorPlan = new ProjectInvestorPlan();
					projectInvestorPlan.setId(snowflakeIdWorker.nextId());
					projectInvestorPlan.setInvestOrderId(projectInvestOrder.getId());
					projectInvestorPlan.setInvestType(isPackagedProject ? Byte.valueOf("3") : Byte.valueOf("1"));
					projectInvestorPlan.setProjectId(projectInvestOrder.getProjectId());
					projectInvestorPlan.setOrgId(project.getOrgId());
					projectInvestorPlan.setUserId(projectInvestOrder.getUserId());
                    projectInvestorPlan.setAccountId(projectInvestOrder.getAccountId());
                    projectInvestorPlan.setPlatformUserNo(projectInvestOrder.getPlatformUserNo());
					projectInvestorPlan.setCurrentPeriod(projectBorrowerPlanNew.getPeriod());
					projectInvestorPlan.setTotalPeriod(projectBorrowerPlanNew.getTotalPeriod());
					projectInvestorPlan.setReceivedDate(projectBorrowerPlanNew.getRefundDate());
					projectInvestorPlan.setYearMouth(DateFormatUtils.format(projectBorrowerPlanNew.getRefundDate(),
							DatePatternConstant.YYYYMM));
					projectInvestorPlan.setPenaltyBeginDate(projectBorrowerPlanNew.getRefundDate());

					projectInvestorPlan.setInterest(projectInvestOrder.getAmount().multiply(monthRate)
							.setScale(AmountHandleConstant.CALCULATE_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN));

					projectInvestorPlan.setReceivedStatus(InvestorPlanReceivedStatusEnum.NON_RECEIVE.getCode());
					projectInvestorPlan.setInvestTime(projectInvestOrder.getCreateTime());
					projectInvestorPlan.setCreateTime(new Date());
					projectInvestorPlan.setUpdateTime(new Date());

					// 最后一期还本金
					if (projectBorrowerPlanNew.getPeriod() == projectBorrowerPlanNewList.size()) {
						projectInvestorPlan.setCapital(projectInvestOrder.getAmount());
					} else {
						projectInvestorPlan.setCapital(BigDecimal.ZERO);
					}

					projectInvestorPlanMapper.insertSelective(projectInvestorPlan);

					result.add(projectInvestorPlan);

					if (investCapitalTotal.get(projectBorrowerPlanNew.getPeriod()) == null) {
						investCapitalTotal.put(projectBorrowerPlanNew.getPeriod(), projectInvestorPlan.getCapital());
						investInterestTotal.put(projectBorrowerPlanNew.getPeriod(), projectInvestorPlan.getInterest());
					} else {
						investCapitalTotal.put(projectBorrowerPlanNew.getPeriod(), investCapitalTotal.get(projectBorrowerPlanNew.getPeriod()).add(projectInvestorPlan.getCapital()));
						investInterestTotal.put(projectBorrowerPlanNew.getPeriod(), investInterestTotal.get(projectBorrowerPlanNew.getPeriod()).add(projectInvestorPlan.getInterest()));
					}

					if (1 == projectInvestorPlan.getCurrentPeriod()) {
						firstReceivedDate = projectInvestorPlan.getReceivedDate();
						firstInterest = projectInvestorPlan.getInterest();
					}
					if (project.getDeadline() == projectInvestorPlan.getCurrentPeriod()) {
						expectOverDate = projectInvestorPlan.getReceivedDate();
					}
					investorInterest = investorInterest.add(projectInvestorPlan.getInterest());
				}

				InvestorCreditor investorCreditor = buildInvestorCreditor(snowflakeIdWorker.nextId(), project, projectInvestOrder, firstReceivedDate, firstInterest, expectOverDate, projectInvestOrder.getAmount(),investorInterest);
				investorCreditorMapperExt.insertSelective(investorCreditor);
			}
		}
		/**
		 * 更新借款人应还款本金为投资人应收款本金之和,应还款利息为投资人应收款利息之和
		 */
		updateBorrowerCapitalInterest(projectBorrowerPlanDetailMapperExt, investCapitalTotal, investInterestTotal, projectBorrowerPlanNewList);
		return result;
	}
}
